﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using System.Xml.Linq;


namespace MDK
{
    public partial class Option : Form
    {
        private XmlDocument xmlDoc = new XmlDocument();
        public string UV4Path
        {
            set
            {
                RegWrite("UV4Path", this.UV4Path);
            }
            get
            {
                return RegRead("UV4Path");
            }
        }
        public string DefaultVSVersion
        {
            set
            {
                RegWrite("DefaultVSVersion", this.DefaultVSVersion);
            }
            get
            {
                return RegRead("DefaultVSVersion");
            }
        }
        public string ToolVersion
        {
            set
            {
                RegWrite("ToolVersion", this.ToolVersion);
            }
            get
            {
                return RegRead("ToolVersion");
            }
        }

        public string VSProjectNewSubFolder
        {
            set
            {
                RegWrite("VSProjectNewSubFolder", this.VSProjectNewSubFolder);
            }
            get
            {
                return RegRead("VSProjectNewSubFolder");
            }
        }
        private string ToolName = "MDK Project Convert To Visual Studio Project";
        private string strConfigFileName = "Config.xml";
        Microsoft.Win32.RegistryKey keyCurrentUser;
        Microsoft.Win32.RegistryKey keyMDKPlugin;


        private string RegRead(string name)
        {
            object objResult = keyMDKPlugin.GetValue(name);
            if (objResult==null)
            {
                return "";
            }
            return objResult.ToString();
        }
        private void RegWrite(string name,string value)
        {
            keyMDKPlugin.SetValue(name, value, Microsoft.Win32.RegistryValueKind.String);
        }
        private void Init()
        {
            keyCurrentUser = Microsoft.Win32.Registry.CurrentUser;
            try
            {
                keyMDKPlugin = keyCurrentUser.OpenSubKey("Software\\MDKPlugin"); //该项必须已存在
            }
            catch
            {
                RegCreate();
            }
            if(RegRead("ToolVersion")== "")
            {
                RegCreate();
            }
        }


        private void RegCreate()
        {

            keyMDKPlugin = keyCurrentUser.CreateSubKey("Software\\MDKPlugin"); //该项必须已存在
            keyMDKPlugin.SetValue("MDKPlugin", "博客园");
            //创建HKEY_CURRENT_USER\Software\MDKPlugin表项”
            // 注意：SetValue()还有第三个参数，主要是用于设置键值的类型，如：字符串，二进制，Dword等等~~默认是字符串。如：
            keyMDKPlugin.SetValue("ToolVersion", "1.0.0", Microsoft.Win32.RegistryValueKind.String); //二进制信息
            keyMDKPlugin.SetValue("UV4Path", UV4Path, Microsoft.Win32.RegistryValueKind.String);
            keyMDKPlugin.SetValue("DefaultVSVersion", DefaultVSVersion, Microsoft.Win32.RegistryValueKind.String);
            keyMDKPlugin.SetValue("VSProjectNewSubFolder", VSProjectNewSubFolder, Microsoft.Win32.RegistryValueKind.String);
            keyCurrentUser.Close();
        }

        public Option()
        {
            InitializeComponent();
            Init();
        }

        /// <summary>
        /// 获取Keil配置信息
        /// </summary>
        /// <param name="DocName"></param>

        public void ReadConfig()
        {
            string DocName = Application.StartupPath + strConfigFileName;
            if (File.Exists(DocName))
            {
                XNamespace ns = "jtl605@163.com";
                XDocument xDocument = XDocument.Load(DocName);
                UV4Path = xDocument.Root.Element(ns + "UV4Path").Value;
                return;
            }
        }

        private void Creat_Config()
        {
            string DocName = Application.StartupPath + strConfigFileName;

            if (DocName == "")
            {
                return;
            }
            
            ToolVersion = "0.1";
            XNamespace ns = "jtl605@163.com";
            XElement xElement = new XElement(ns + "Tool", new object[]
			{
				new XAttribute("Name", ToolName),
				new XAttribute("Author", "Ka_Chen"),
				new XAttribute("ToolsVersion", ToolVersion),
				new XElement(ns + "UV4Path", UV4Path)
			});
            xElement.Save(DocName);
        }

        private void Option_Load(object sender, EventArgs e)
        {
        }




    }
}
